#include "stdafx.h"
#include "moretunes.h"
#include "AlbumsTab.h"
#include "misc.h"
#include ".\albumstab.h"
#include "shareware.h"


// URLs
#define	DEFAULT_URL	"http://www.amazon.com/exec/obidos/redirect-home/moretunes03-20"
#define	BUY_ADDRESS	"http://www.amazon.com/o/dt/assoc/handle-buy-box="

void CAlbumsTab::OnBnClickedFind() {
	if (_searching == TRUE) {
		//stop downloading
		_allTabs->_grabPicList._stop = TRUE;
		_allTabs->_grabXmlList._stop = TRUE;
		_allTabs->_grabPicList.killThread();
		_allTabs->_grabXmlList.killThread();
		return;
	}
	CString name;
	_editFind.GetWindowText(name);
	if (triggerDownload(name)) {
		_editFind.SetSel(NULL, name.GetLength());
	}
}

void CAlbumsTab::OnBnClickedScanPl() {
	CMenu menu ;
	VERIFY(menu.LoadMenu(IDR_LYRICS));
	CMenu* pPopup = menu.GetSubMenu (0) ;
	ASSERT(pPopup != NULL);
	// Get the cursor position
	POINT pt ;
	GetCursorPos (&pt) ;
	pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,pt.x, pt.y, AfxGetMainWnd());	
}

void CAlbumsTab::OnScan(void)
{
	scanPlaylist();
}

void CAlbumsTab::OnBuyNew(void)
{
	if (!_currentAlbum) {
		ShellExecute(NULL, "open", DEFAULT_URL, NULL, NULL, SW_SHOWNORMAL);	
		return;
	}
	CString temp = BUY_ADDRESS;
	temp += _currentAlbum->getAsin();
	ShellExecute(NULL, "open", temp, NULL, NULL, SW_SHOWNORMAL);	
}

void CAlbumsTab::OnBuyUsed(void) {
	this->OnBuyNew();
}

/////////////////////////////////////////////////
// Google Stuffs
/////////////////////////////////////////////////
#define URL_GOOGLE_BIO "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=biography+%22"
void CAlbumsTab::OnBio(void) {
	googleQuery(URL_GOOGLE_BIO, toHtml(_ID3Tab->_artistData));
}
#define URL_GOOGLE_BIO1 "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=site:www.wikipedia.org+%22"
void CAlbumsTab::OnBio1(void) {
	googleQuery(URL_GOOGLE_BIO1, toHtml(_ID3Tab->_artistData));
}

#define URL_GOOGLE_LYRICS "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=lyrics+lyrics+%22"
void CAlbumsTab::OnLyrics(void) {
	CString keys;
	keys = toHtml(_ID3Tab->_artistData);
	keys += "%22+%22";
	keys += toHtml(_ID3Tab->_titleData);
	googleQuery(URL_GOOGLE_LYRICS, keys);
}
#define URL_GOOGLE_LYRICS1 "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=site:www.lyrics007.com+lyrics+%22"
void CAlbumsTab::OnLyrics1(void) {
	CString keys;
	keys = toHtml(_ID3Tab->_artistData);
	keys += "%22+%22";
	keys += toHtml(_ID3Tab->_titleData);
	googleQuery(URL_GOOGLE_LYRICS1, keys);
}
#define URL_GOOGLE_LYRICS2 "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=site:www.sing365.com+lyrics+%22"
void CAlbumsTab::OnLyrics2(void) {
	CString keys;
	keys = toHtml(_ID3Tab->_artistData);
	keys += "%22+%22";
	keys += toHtml(_ID3Tab->_titleData);
	googleQuery(URL_GOOGLE_LYRICS2, keys);
}


#define SHAREWARE_RANDOM 10
#define URL_GOOGLE_END "%22&btnI=I%27m+Feeling+Lucky"
int CAlbumsTab::googleQuery(CString googleBase, CString keys) {
	if (rand()%SHAREWARE_RANDOM == 0 && _prefs->_registration != REGISTRATION_OK) {
		CShareware shareware;
		shareware.DoModal();
	}
	CString url;
	url += googleBase;
	url += keys;
	url += URL_GOOGLE_END;
	ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);	
	return TRUE;
}

//////////////////////////////////////////////////////
// More On Whatever
//////////////////////////////////////////////////////


#define MESSAGE_DOWNLOAD_BAD "The P2P client set in the preferences isn't available on this computer"
void CAlbumsTab::OnDownload(void) {
	HINSTANCE hInst = ShellExecute(NULL, "open", _prefs->_editP2PData, NULL, NULL, SW_SHOWNORMAL);
	int retVal = (int) hInst;
	if (retVal == ERROR_FILE_NOT_FOUND || retVal == 0 || retVal == ERROR_PATH_NOT_FOUND 
		|| retVal == ERROR_BAD_FORMAT)
		AfxMessageBox(MESSAGE_DOWNLOAD_BAD, MB_ICONINFORMATION);
}

void CAlbumsTab::OnSimilar(void) {
	// Can we search and inits
	if (_searching || !_currentAlbum)
		return;
	// We pass the name to alltabs which is the albumManager
	_searching = TRUE;
	_searchButton.SetWindowText(BUTTON_SEARCH_STOP);
	_allTabs->downloadSimilar(_currentAlbum);
}

void CAlbumsTab::OnProposal(void) {
	_proposalAlbum = _currentAlbum;
	_ID3Tab->setProposal(_proposalAlbum);
}

void CAlbumsTab::OnBnClickedProposal() {
	OnProposal();
	_allTabs->SetActivePage(TAB_ID3);
}

void CAlbumsTab::OnBnClickedMore() {
	CMenu menu ;
	VERIFY(menu.LoadMenu(IDR_MENU_MORE));
	CMenu* pPopup = menu.GetSubMenu (0) ;
	ASSERT(pPopup != NULL);
	// Get the cursor position
	POINT pt ;
	GetCursorPos (&pt) ;
	pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,pt.x, pt.y, AfxGetMainWnd());
}
void CAlbumsTab::OnBnClickedBuy()
{
	CMenu menu;
	menu.CreatePopupMenu();
	menu.AppendMenu(MF_POPUP | MF_STRING, ID_BUY_NEW, (LPCTSTR)"Buy New");	
	menu.AppendMenu(MF_POPUP | MF_STRING, ID_BUY_USED, (LPCTSTR)"Buy Used");
	POINT pt;
	::GetCursorPos(&pt);
	menu.TrackPopupMenu(TPM_CENTERALIGN, pt.x, pt.y, this);
}


////////////////////////////////////////////////////////////////////////////
// Sort functions
////////////////////////////////////////////////////////////////////////////


int CALLBACK CAlbumsTab::MyCompareProcMatch(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// Get the two albums we want to compare
	Album* pItem1 = (Album*) lParam1;
	Album* pItem2 = (Album*) lParam2;
	if (pItem1 == NULL || pItem2 == NULL)
		return NULL;
	else
		return pItem1->_match <= pItem2->_match;
}


int CALLBACK CAlbumsTab::MyCompareTracks(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// Get the two albums we want to compare
	CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
//	CString    strItem1 = pListCtrl->GetItemText(lParam1, 1);
//	CString    strItem2 = pListCtrl->GetItemText(lParam2, 1);
//	return strcmp(strItem1, strItem2);
// have to update the red one!
	return 1;
}

int CALLBACK CAlbumsTab::MyCompareTracksD(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// Get the two albums we want to compare
	Album* pItem1 = (Album*) lParam1;
	Album* pItem2 = (Album*) lParam2;

	if (pItem1 == NULL || pItem2 == NULL)
		return NULL;
	if (lParamSort == 0)
		return -strcmp(pItem1->getTitle(), pItem2->getTitle());
	if (lParamSort == 1)
		return -strcmp(pItem1->getArtist(), pItem2->getArtist());
	if (lParamSort == 2)
		return -(pItem1->getTracksSize() - pItem2->getTracksSize());
	if (lParamSort == 3)
		return -strcmp(pItem1->getDateYear(), pItem2->getDateYear());
	return 1;
}

int CALLBACK CAlbumsTab::MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// Get the two albums we want to compare
	Album* pItem1 = (Album*) lParam1;
	Album* pItem2 = (Album*) lParam2;
	if (pItem1 == NULL || pItem2 == NULL)
		return NULL;
	if (lParamSort == 0)
		return strcmp(pItem1->getTitle(), pItem2->getTitle());
	if (lParamSort == 1)
		return strcmp(pItem1->getArtist(), pItem2->getArtist());
	if (lParamSort == 2)
		return (pItem1->getTracksSize() - pItem2->getTracksSize());
	if (lParamSort == 3)
		return strcmp(pItem1->getDateYear(), pItem2->getDateYear());
	return 1;
}

int CALLBACK CAlbumsTab::MyCompareProcD(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// Get the two albums we want to compare
	Album* pItem1 = (Album*) lParam1;
	Album* pItem2 = (Album*) lParam2;

	if (pItem1 == NULL || pItem2 == NULL)
		return NULL;
	if (lParamSort == 0)
		return -strcmp(pItem1->getTitle(), pItem2->getTitle());
	if (lParamSort == 1)
		return -strcmp(pItem1->getArtist(), pItem2->getArtist());
	if (lParamSort == 2)
		return -(pItem1->getTracksSize() - pItem2->getTracksSize());
	if (lParamSort == 3)
		return -strcmp(pItem1->getDateYear(), pItem2->getDateYear());
	return 1;
}



/////////////////////////////////////////////////////////////
// event stuffs
/////////////////////////////////////////////////////////////

void CAlbumsTab::DoDataExchange(CDataExchange* pDX)
{
	CResizablePage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, _editFind);
	DDX_Control(pDX, IDC_EDIT2, _artist);
	DDX_Control(pDX, IDC_LIST1, _trackList);
	DDX_Control(pDX, IDC_LIST2, _albumList);
	DDX_Control(pDX, IDC_EDIT4, _date);
	DDX_Control(pDX, IDC_EDIT5, _trackNum);
	DDX_Control(pDX, IDC_EDIT6, _price);
	DDX_Control(pDX, IDC_EDIT7, _rating);
	DDX_Control(pDX, IDC_M_PIC, _pic);
	DDX_Control(pDX, IDC_EDIT3, _genre);
	DDX_Control(pDX, IDC_BUTTON1, _searchButton);
}


BEGIN_MESSAGE_MAP(CAlbumsTab, CResizablePage)
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedFind)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedMore)
//	ON_NOTIFY(NM_RCLICK, IDC_LIST1, OnLvnItemActivateTracksList)
	ON_NOTIFY(LVN_ITEMACTIVATE, IDC_LIST2, OnLvnItemActivateAlbumsList)
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnLvnColumnclickTracksList)
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST2, OnLvnColumnclickAlbumsList)
	ON_NOTIFY(LVN_KEYDOWN, IDC_LIST2, OnLvnKeydownAlbumList)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedBuy)
	ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedScanPl)

	ON_COMMAND(ID_BUY_NEW, OnBuyNew)
	ON_COMMAND(ID_BUY_USED, OnBuyUsed)

	ON_COMMAND(ID_RELOAD, OnReload)
	ON_COMMAND(ID_SIMILAR, OnSimilar)
	ON_COMMAND(ID_DOWNLOAD, OnDownload)
	ON_COMMAND(ID_PROPOSAL, OnProposal)
	ON_COMMAND(ID_LYRICS, OnLyrics)
	ON_COMMAND(ID_LYRICS1, OnLyrics1)
	ON_COMMAND(ID_LYRICS2, OnLyrics2)
	ON_COMMAND(ID_BIO, OnBio)
	ON_COMMAND(ID_BIO1, OnBio1)
	ON_COMMAND(ID_SCAN, OnScan)

	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedProposal)
	ON_WM_SIZE()
	ON_WM_LBUTTONDBLCLK()
	ON_COMMAND(ID__COPYINFOTOCLIPBOARD, copyToClipboard)
//	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnNMClickTracks)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnNMDblclkTracks)
	ON_NOTIFY(NM_RCLICK, IDC_LIST1, OnNMRclickList1)
END_MESSAGE_MAP()
